<style lang="scss" scoped>
  .tier-list {

  }
</style>
<template>
  <div class="tier-list">
    <div class="page-container">
      <el-table
        @row-click="rowClick"
        :data="originData"

        style="width: 100%">
        <el-table-column width="50">
          <template scope="scope">
            <span v-if="scope.row.parentId=='root'"> <i class="el-icon-arrow-down" v-if="scope.row.isOpen"></i>
            <i class="el-icon-arrow-right" v-if="!scope.row.isOpen"></i></span>

          </template>
        </el-table-column>
        <el-table-column
          label="Tier"
        >
          <template scope="scope">
            <el-button type="text" size="small">
              <router-link :to="{
              name: 'tier-detail',
              query:{ tierId: scope.row.parentId=='root'?scope.row.id:scope.row.parentId,instanceId:scope.row.parentId=='root'?'':scope.row.id }
            }">{{scope.row.name}}






              </router-link>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="平均响应时间"
          sortable
          prop="avg">
        </el-table-column>
        <el-table-column
          label="吞吐量"
          prop="tps">
        </el-table-column>
        <el-table-column
          label="错误率"
          prop="error">
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
  import {
    Row,
    Col,
    Table,
    TableColumn,
    Loading,
    Message,
    Button
  } from 'element-ui'
  export default {
    name: 'tree-table',
    components: {
      'el-row': Row,
      'el-col': Col,
      'el-table': Table,
      'el-table-column': TableColumn,
      'el-button': Button
    },
    props: ['originData'],
    data(){
      return {
        dataList: [],
        loading: true
      }
    },
    mounted(){
      this.dataList = _.cloneDeep(this.format(this.originData));
    },
    updated(){
      console.info(this.dataList)
    },
    methods: {
      format(oData){
        oData.map(m => {
          m.isOpen = false;
          m.parentId = 'root';
          !_.isEmpty(m.child) && m.child.map(mm => {
            mm.parentId = m.id
          })
        });

        return oData
      },
      addRow(rowId){
        let allDataList = this.data;
        let childrenList = allDataList.find(m => m.id == rowId).children;
        let addIndex = _.findIndex(this.dataList, m => m.id == rowId);
        this.dataList.splice(addIndex + 1, 0, ...childrenList);
      },
      delRow(rowId){
        this.dataList = _.remove(this.dataList, m => m.parentId != rowId);
      },
      toggleOpen(rowId){
        this.dataList.map(m => {
          if (m.id == rowId) {
            m.isOpen = !m.isOpen
          }
        })
      },
      rowClick(rowData){
        if (rowData.parentId != 'root') return;
        rowData.isOpen ? this.delRow(rowData.id) : this.addRow(rowData.id);
        this.toggleOpen(rowData.id)
      }
    }
  }
</script>
